//
//  AnimeDetallsViewController.m
//  GentlemanAnime
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "AnimeDetallsViewController.h"
#import "DetaCollectionViewCell.h"
#import "TuijianCollectionViewCell.h"
#import "HTTPTool.h"
#import "EpisodeModel.h"
#import "DetailsInfoModel.h"
#import "DetailsListModel.h"
#import "DetailsSimilarModel.h"
#import "UIImageView+WebCache.h"
#import "AnimeReadViewController.h"
#import "MBProgressHUD.h"
#import "DownloadSingletonTool.h"
#import "AppDelegate.h"
#import "LeftCollectionViewCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

typedef NS_ENUM(NSInteger, AnimeStyle) {
    listStyle,
    similarStyle,
};

@interface AnimeDetallsViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UIScrollView *detaScrollView;
@property(nonatomic, retain)UICollectionView *detaCollectionView;
@property(nonatomic, retain)UICollectionView *tuijianCollectionView;
@property(nonatomic, retain)NSMutableArray *listArr;
@property(nonatomic, retain)NSMutableArray *similarArr;
@property(nonatomic, retain)DetailsInfoModel *infoModel;
@property(nonatomic, retain)DetailsSimilarModel *similarModel;
@property(nonatomic, assign)NSInteger detailsStyle;
@property(nonatomic, retain)UICollectionViewFlowLayout *detailsFlowLayout;
@property(nonatomic, retain)UICollectionViewFlowLayout *tuijianFlowLayout;
@property(nonatomic, retain)UIView *daView;
@property(nonatomic, retain)UIView *kuangView;
@property(nonatomic, retain)UIImageView *coverImageView;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *authorsLabel;
@property(nonatomic, retain)UILabel *countryLabel;
@property(nonatomic, retain)UILabel *zoneLabel;
@property(nonatomic, retain)UILabel *zhuangtaiLabel;
@property(nonatomic, retain)UILabel *statusLabel;
@property(nonatomic, retain)UILabel *zuixinLabel;
@property(nonatomic, retain)UILabel *jishuLabel;
@property(nonatomic, retain)UIButton *shoucangButton;
@property(nonatomic, retain)UIButton *yueduButton;
@property(nonatomic, retain)UILabel *xqLabel;
@property(nonatomic, retain)UILabel *detailsLabel;
@property(nonatomic, retain)UIView *backView;
@property(nonatomic, retain)UIView *fenView;
@property(nonatomic, retain)UIButton *manhuaButton;
@property(nonatomic, retain)UIButton *tuijianButton;
@property(nonatomic, retain)UILabel *lianzaiLabel;
@property(nonatomic, retain)UILabel *paixuLabel;
@property(nonatomic, retain)UILabel *clickLabel;
@property(nonatomic, retain)UILabel *shouqiLabel;
@property(nonatomic, retain)UIButton *zhengButton;
@property(nonatomic, retain)UIButton *daoButton;
@property(nonatomic, assign)BOOL isZheng;
@property(nonatomic, assign)BOOL isReturn;
@property(nonatomic, assign)BOOL isArchiver;
@property(nonatomic, retain)UIView *clickView;
@property(nonatomic, retain)UIImageView *openImageView;
@property(nonatomic, retain)UIImageView *closeImageView;
@property(nonatomic, retain)UIView *siGeKongjianView;
@property(nonatomic, retain)UIView *flowLatoutView;
@property(nonatomic, retain)NSMutableArray *animeArr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, retain)UIView *upView;

@end

@implementation AnimeDetallsViewController

- (void)dealloc
{
    [_listArr release];
    [_similarArr release];
    [_detaScrollView release];
    [_detaCollectionView release];
    [_tuijianCollectionView release];
    [_infoModel release];
    [_detailsFlowLayout release];
    [_tuijianFlowLayout release];
    [_siGeKongjianView release];
    [_daView release];
    [_kuangView release];
    [_coverImageView release];
    [_nameLabel release];
    [_authorsLabel release];
    [_countryLabel release];
    [_zoneLabel release];
    [_zhuangtaiLabel release];
    [_statusLabel release];
    [_zuixinLabel release];
    [_jishuLabel release];
    [_xqLabel release];
    [_detailsLabel release];
    [_backView release];
    [_fenView release];
    [_lianzaiLabel release];
    [_paixuLabel release];
    [_clickLabel release];
    [_shouqiLabel release];
    [_clickView release];
    [_openImageView release];
    [_closeImageView release];
    [_flowLatoutView release];
    [_animeArr release];
    [_upView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailsStyle = listStyle;
        self.listArr  = [NSMutableArray array];
        self.similarArr  = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UILabel *titlelebel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    titlelebel.text = self.tuTitle;
    titlelebel.font = [UIFont boldSystemFontOfSize:18];
    titlelebel.textAlignment = NSTextAlignmentCenter;
    titlelebel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titlelebel;
    [titlelebel release];

    [self fanguidang];
    
    self.isZheng = NO;
    self.isReturn = YES;
//    [self createData];
    
    // 建立左右两个button

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 60, 30);
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(5, 0, 5, 40);
    [leftButton setImage:[UIImage imageNamed:@"单箭头"] forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:16];
    leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, - 20, 0, 0);
    leftButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [leftButton addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    

    
    // 建立一个瀑布流
    self.detailsFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.detailsFlowLayout.itemSize = CGSizeMake((WIDTH - 40) / 4, (WIDTH - 40) / 4 * 30 / 83);
    self.detailsFlowLayout.minimumLineSpacing = 10;
    self.detailsFlowLayout.minimumInteritemSpacing = 10;
    self.detailsFlowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 69, 5);
    
    // 创建CollectionView
    self.detaCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:self.detailsFlowLayout];
    self.detaCollectionView.backgroundColor = [UIColor whiteColor];
    self.detaCollectionView.dataSource = self;
    self.detaCollectionView.delegate = self;
    self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 285 / 320, 0, 0, 0);
    [self.view addSubview:self.detaCollectionView];
    [_detaCollectionView release];
    
    // 注册
    [self.detaCollectionView registerClass:[DetaCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.detaCollectionView registerClass:[TuijianCollectionViewCell class] forCellWithReuseIdentifier:@"tuijianReuse"];
    
    self.upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.upView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.upView];
    [_upView release];

    [self createData];

// ========================================================
    // 创建headerView
    self.daView = [[UIView alloc] initWithFrame:CGRectMake(0, -WIDTH * 285 / 320, WIDTH, WIDTH * 300 / 320)];
//    self.daView.backgroundColor = [UIColor yellowColor];
    [self.detaCollectionView addSubview:self.daView];
    [_daView release];
    
    self.kuangView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 10 / 320, WIDTH * 10 / 320, WIDTH * 100 / 320, WIDTH * 140 / 320)];
    self.kuangView.backgroundColor = [UIColor whiteColor];
    self.kuangView.layer.borderWidth = 1;
    self.kuangView.layer.borderColor = [[UIColor colorWithWhite:0.600 alpha:1.000] CGColor];
    [self.daView addSubview:self.kuangView];
    [_kuangView release];
    
    self.coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 2 / 320, WIDTH * 2 / 320, WIDTH * 96 / 320, WIDTH * 136 / 320)];
//    self.coverImageView.backgroundColor = [UIColor cyanColor];
    [self.kuangView addSubview:self.coverImageView];
    [_coverImageView release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 120 / 320, HEIGHT * 10 / 480, WIDTH * 30 / 320, WIDTH * 25 / 320)];
    self.nameLabel.text = @"作者:";
//    self.nameLabel.backgroundColor = [UIColor yellowColor];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    [self.daView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.authorsLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 150 / 320, WIDTH * 10 / 320, WIDTH * 160 / 320, WIDTH * 25 / 320)];
    self.authorsLabel.font = [UIFont systemFontOfSize:12];
    [self.daView addSubview:self.authorsLabel];
    [_authorsLabel release];
    
    self.countryLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 120 / 320, WIDTH * 35 / 320, WIDTH * 30 / 320, WIDTH * 25 / 320)];
    self.countryLabel.font = [UIFont systemFontOfSize:12];
    self.countryLabel.text = @"地域:";
//    self.countryLabel.backgroundColor = [UIColor yellowColor];
    [self.daView addSubview:self.countryLabel];
    [_countryLabel release];
    
    self.zoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 150 / 320, WIDTH * 35 / 320, WIDTH * 160 / 320, WIDTH * 25 / 320)];
    self.zoneLabel.font = [UIFont systemFontOfSize:12];
    [self.daView addSubview:self.zoneLabel];
    [_zoneLabel release];
    
    self.zhuangtaiLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 120 / 320, WIDTH * 60 / 320, WIDTH * 30 / 320, WIDTH * 25 / 320)];
    self.zhuangtaiLabel.font = [UIFont systemFontOfSize:12];
    self.zhuangtaiLabel.text = @"状态:";
//    self.zhuangtaiLabel.backgroundColor = [UIColor yellowColor];
    [self.daView addSubview:self.zhuangtaiLabel];
    [_zhuangtaiLabel release];
    
    self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 150 / 320, WIDTH * 60 / 320, WIDTH * 160 / 320, WIDTH * 25 / 320)];
    self.statusLabel.font = [UIFont systemFontOfSize:12];
    [self.daView addSubview:self.statusLabel];
    [_statusLabel release];
    
    self.zuixinLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 120 / 320, WIDTH * 85 / 320, WIDTH * 55 / 320, WIDTH * 25 / 320)];
    self.zuixinLabel.font = [UIFont systemFontOfSize:12];
    self.zuixinLabel.text = @"最新收录:";
//    self.zuixinLabel.backgroundColor = [UIColor yellowColor];
    [self.daView addSubview:self.zuixinLabel];
    [_zuixinLabel release];
    
    self.jishuLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 175 / 320, WIDTH * 85 / 320, WIDTH * 135 / 320, WIDTH * 25 / 320)];
    self.jishuLabel.font = [UIFont systemFontOfSize:12];
    [self.daView addSubview:self.jishuLabel];
    [_jishuLabel release];
    
    self.shoucangButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.shoucangButton.frame = CGRectMake(WIDTH * 120 / 320, WIDTH * 120 / 320, WIDTH * 85 / 320, WIDTH * 30 / 320);
    self.shoucangButton.backgroundColor = [UIColor colorWithRed:0.502 green:0.502 blue:0.000 alpha:1.000];
    self.shoucangButton.layer.cornerRadius = 5;
    self.shoucangButton.titleLabel.font = [UIFont systemFontOfSize:14];\
    // 如果没收藏就是第二张,收藏了就是第一张
    if (self.isArchiver) {
        [self.shoucangButton setTitle:@"已收藏" forState:UIControlStateNormal];
    }else{
        [self.shoucangButton setTitle:@"添加收藏" forState:UIControlStateNormal];
    }
    [self.shoucangButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.daView addSubview:self.shoucangButton];
    [self.shoucangButton addTarget:self action:@selector(shoucangClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.yueduButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.yueduButton.frame = CGRectMake(WIDTH * 215 / 320, WIDTH * 120 / 320, WIDTH * 85 / 320, WIDTH * 30 / 320);
    self.yueduButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    self.yueduButton.layer.cornerRadius = 5;
    self.yueduButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.yueduButton setTitle:@"开始阅读" forState:UIControlStateNormal];
    [self.yueduButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.daView addSubview:self.yueduButton];
    [self.yueduButton addTarget:self action:@selector(yueduClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.xqLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 10 / 320, WIDTH * 165 / 320, WIDTH * 100 / 320, WIDTH * 25 / 320)];
    self.xqLabel.font = [UIFont systemFontOfSize:12];
    self.xqLabel.text = @"详细介绍";
    [self.daView addSubview:self.xqLabel];
    [_xqLabel release];
    
    self.detailsLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, WIDTH * 12 / 320)];
    self.detailsLabel.font = [UIFont systemFontOfSize:12];
//    self.detailsLabel.backgroundColor = [UIColor yellowColor];
    [self.daView addSubview:self.detailsLabel];
    [_detailsLabel release];
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, WIDTH * 230 / 320, WIDTH, WIDTH * 30 / 320)];
    self.backView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self.daView addSubview:self.backView];
    [_backView release];
    
    self.fenView = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2, WIDTH * 5 / 320, WIDTH * 1 / 320, WIDTH * 20 / 320)];
    self.fenView.backgroundColor = [UIColor grayColor];
    [self.backView addSubview:self.fenView];
    [_fenView release];
    
    self.manhuaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.manhuaButton.frame = CGRectMake(0, 0, WIDTH / 2 - 2, WIDTH * 30 / 320);
    self.manhuaButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.manhuaButton setTitle:@"漫画剧集" forState:UIControlStateNormal];;
    [self.manhuaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.backView addSubview:self.manhuaButton];
    [self.manhuaButton addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside];
    self.manhuaButton.tag = 1002;
    
    self.tuijianButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.tuijianButton.frame = CGRectMake(WIDTH / 2 + 2, 0, WIDTH / 2 - 2, WIDTH * 30 / 320);
    self.tuijianButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.tuijianButton setTitle:@"相关推荐" forState:UIControlStateNormal];;
    [self.tuijianButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.backView addSubview:self.tuijianButton];
    [self.tuijianButton addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside];
    self.tuijianButton.tag = 1003;
    
    self.siGeKongjianView = [[UIView alloc] initWithFrame:CGRectMake(0, WIDTH * 260 / 320, WIDTH, WIDTH * 30 / 320)];
//    self.siGeKongjianView.backgroundColor = [UIColor yellowColor];
    [self.daView addSubview:self.siGeKongjianView];
    [_siGeKongjianView release];
    
    self.lianzaiLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 10 / 320, 0, 45, 30)];
    self.lianzaiLabel.font = [UIFont systemFontOfSize:12];
    self.lianzaiLabel.text = @"连载(话)";
    self.lianzaiLabel.textAlignment = NSTextAlignmentCenter;
    [self.siGeKongjianView addSubview:self.lianzaiLabel];
    [_lianzaiLabel release];
    
    self.paixuLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 140, 0, WIDTH * 55 / 320, WIDTH * 30 / 320)];
    self.paixuLabel.font = [UIFont systemFontOfSize:12];
    self.paixuLabel.text = @"排序方式:";
    self.paixuLabel.textAlignment = NSTextAlignmentCenter;
    [self.siGeKongjianView addSubview:self.paixuLabel];
    [_paixuLabel release];
    
    self.zhengButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.zhengButton.frame = CGRectMake(WIDTH - 80, WIDTH * 5 / 320, WIDTH * 30 / 320, WIDTH * 20 / 320);
    self.zhengButton.layer.cornerRadius = 3;
    self.zhengButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.zhengButton setTitle:@"正序" forState:UIControlStateNormal];
    [self.zhengButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.siGeKongjianView addSubview:self.zhengButton];
    self.zhengButton.tag = 1000;
    [self.zhengButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.daoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.daoButton.frame = CGRectMake(WIDTH - 40, WIDTH * 5 / 320, WIDTH * 30 / 320, WIDTH * 20 / 320);
    self.daoButton.layer.cornerRadius = 3;
    self.daoButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.daoButton setTitle:@"倒序" forState:UIControlStateNormal];
    [self.daoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.daoButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [self.siGeKongjianView addSubview:self.daoButton];
    self.daoButton.tag = 1001;
    [self.daoButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.clickView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH - WIDTH * 90 / 320, WIDTH * 205 / 320, WIDTH * 80 / 320, WIDTH * 20 / 320)];
    self.clickView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:0.620];
    self.clickView.layer.cornerRadius = 5;
    self.clickView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.clickView addGestureRecognizer:tap];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [self.daView addSubview:self.clickView];
    [_clickView release];
    
    self.openImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"展开"]];
    self.openImageView.frame = CGRectMake(WIDTH * 5 / 320, WIDTH * 3 / 320, 15, 15);
    [self.clickView addSubview:self.openImageView];
    [_openImageView release];
    
    self.clickLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 20 / 320, 0, WIDTH * 60 / 320, WIDTH * 20 / 320)];
    self.clickLabel.font = [UIFont systemFontOfSize:14];
    self.clickLabel.text = @"展开全部";
    self.clickLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    [self.clickView addSubview:self.clickLabel];
    
    // 小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.color = [UIColor colorWithRed:0.334 green:0.619 blue:1.000 alpha:1.000];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
}

#pragma mark 解析数据
- (void)createData
{
    NSString *strUrl = [NSString stringWithFormat:@"http://api.dmzj.com/dynamic/comicinfo/%@.json", self.Id];
    [HTTPTool get:strUrl body:nil httpResultStyle:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSDictionary *dataDic = dic[@"data"];
        self.infoModel = [[DetailsInfoModel alloc] init];
        [self.infoModel setValuesForKeysWithDictionary:dataDic[@"info"]];
        
        NSArray *listArr = dataDic[@"list"];
        for (NSDictionary *tempDic in listArr) {
            DetailsListModel *listModel = [[DetailsListModel alloc] init];
            [listModel setValuesForKeysWithDictionary:tempDic];
            [self.listArr addObject:listModel];
            [listModel release];
        }

        NSArray *similarArr = dataDic[@"similar"];
        for (NSDictionary *tempDic in similarArr) {
            DetailsSimilarModel *similarModel = [[DetailsSimilarModel alloc] init];
            [similarModel setValuesForKeysWithDictionary:tempDic];
            [self.similarArr addObject:similarModel];
            [similarModel release];
        }
        [self yemianfuzhi];
        [self.detaCollectionView reloadData];
        [self.tuijianCollectionView reloadData];
        self.upView.hidden = YES;
        [self.upView removeFromSuperview];
        [self.HUD hide:YES];

    } failure:^(NSError *error) {
        
        
    }];
}

#pragma mark 页面赋值
- (void)yemianfuzhi
{
    // 接收数据
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.cover] placeholderImage:[UIImage imageNamed:@"漫画占位图"]];
    self.authorsLabel.text = self.infoModel.authors;
    self.zoneLabel.text = self.infoModel.zone;
    self.statusLabel.text = self.infoModel.status;
    self.jishuLabel.text = self.infoModel.last_update_chapter_name;
    self.detailsLabel.text = self.infoModel.xiangqing;
}

#pragma mark 排序button点击方法
- (void)click:(UIButton *)button
{
    if (button.tag == 1000) {
        if (self.isZheng == NO) {
            self.isZheng = YES;
            [self.zhengButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.zhengButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
            [self.daoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.daoButton.backgroundColor = [UIColor clearColor];
            [self.detaCollectionView reloadData];
        }
    } else if (button.tag == 1001) {
        if (self.isZheng == YES) {
            self.isZheng = NO;
            [self.daoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.daoButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
            [self.zhengButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.zhengButton.backgroundColor = [UIColor clearColor];
            [self.detaCollectionView reloadData];
        }
    }
}

#pragma mark 展开收起的点击方法
- (void)tapAction:(UIButton *)button
{
    if (self.detailsStyle == listStyle) {
        if (self.isReturn == YES) {
            self.closeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"收起"]];
            self.closeImageView.frame = CGRectMake(5, 3, 15, 15);
            self.clickLabel.text = @"收起全部";
            [self.clickView addSubview:self.closeImageView];
            [_closeImageView release];
            [self.openImageView removeFromSuperview];
            
            // 计算文字的高度
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
            self.detailsLabel.numberOfLines = 0;
            [self.detailsLabel sizeToFit];
            CGRect rect = [self.detailsLabel.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, rect.size.height);
            //
            self.clickView.frame = CGRectMake(WIDTH - 90, WIDTH * 190 / 320 + self.detailsLabel.frame.size.height, WIDTH * 80 / 320, WIDTH * 20 / 320);
            self.backView.frame = CGRectMake(0, WIDTH * 215 / 320 + self.detailsLabel.frame.size.height, WIDTH, WIDTH * 30 / 320);
            self.siGeKongjianView.frame = CGRectMake(0, WIDTH * 245 / 320 + self.detailsLabel.frame.size.height, WIDTH, WIDTH * 30 / 320);
            self.daView.frame = CGRectMake(0, - WIDTH * 270 / 320 - self.detailsLabel.frame.size.height, WIDTH, WIDTH * 270 / 320 + self.detailsLabel.frame.size.height);
            self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 270 / 320 + self.detailsLabel.frame.size.height, 0, 0, 0);
            
            self.detaCollectionView.contentOffset = CGPointMake(0, - self.daView.frame.size.height);
            
            self.isReturn = NO;
            
        }else if (self.isReturn == NO){
            self.openImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"展开"]];
            self.openImageView.frame = CGRectMake(5, 3, 15, 15);
            self.clickLabel.text = @"展开全部";
            [self.clickView addSubview:self.openImageView];
            
            self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, WIDTH * 15 / 320);
            self.backView.frame = CGRectMake(0, WIDTH * 230 / 320, WIDTH, WIDTH * 30 / 320);
            self.siGeKongjianView.frame = CGRectMake(0, WIDTH * 260 / 320, WIDTH, WIDTH * 30 / 320);
            self.clickView.frame = CGRectMake(WIDTH - 90, WIDTH * 205 / 320, WIDTH * 80 / 320, WIDTH * 20 / 320);
            self.daView.frame = CGRectMake(0, -WIDTH * 285 / 320, WIDTH, WIDTH * 285 / 320);
            self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 285 / 320, 0, 0, 0);
            self.detaCollectionView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 113);
            
            [_openImageView release];
            [self.closeImageView removeFromSuperview];
            
            self.isReturn = YES;
        }
    }else if (self.detailsStyle == similarStyle){
        if (self.isReturn == YES) {
            self.closeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"收起"]];
            self.closeImageView.frame = CGRectMake(5, 3, 15, 15);
            self.clickLabel.text = @"收起全部";
            [self.clickView addSubview:self.closeImageView];
            [_closeImageView release];
            [self.openImageView removeFromSuperview];
            
            // 计算文字的高度
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
            self.detailsLabel.numberOfLines = 0;
            [self.detailsLabel sizeToFit];
            CGRect rect = [self.detailsLabel.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, rect.size.height);
            //
            self.clickView.frame = CGRectMake(WIDTH - 90, WIDTH * 190 / 320 + self.detailsLabel.frame.size.height, WIDTH * 80 / 320, WIDTH * 20 / 320);
            self.backView.frame = CGRectMake(0, WIDTH * 215 / 320 + self.detailsLabel.frame.size.height, WIDTH, WIDTH * 30 / 320);
            self.siGeKongjianView.frame = CGRectMake(0, WIDTH * 245 / 320 + self.detailsLabel.frame.size.height, WIDTH, WIDTH * 30 / 320);
            self.daView.frame = CGRectMake(0, - WIDTH * 245 / 320 - self.detailsLabel.frame.size.height, WIDTH, WIDTH * 245 / 320 + self.detailsLabel.frame.size.height);
            self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 245 / 320 + self.detailsLabel.frame.size.height, 0, 0, 0);
            
            self.detaCollectionView.contentOffset = CGPointMake(0, - self.daView.frame.size.height);
            
            self.isReturn = NO;
            
        }else if (self.isReturn == NO){
            self.openImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"展开"]];
            self.openImageView.frame = CGRectMake(5, 3, 15, 15);
            self.clickLabel.text = @"展开全部";
            [self.clickView addSubview:self.openImageView];
            
            self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, WIDTH * 15 / 320);
            self.backView.frame = CGRectMake(0, WIDTH * 230 / 320, WIDTH, WIDTH * 30 / 320);
            self.siGeKongjianView.frame = CGRectMake(0, WIDTH * 260 / 320, WIDTH, WIDTH * 30 / 320);
            self.clickView.frame = CGRectMake(WIDTH - 90, WIDTH * 205 / 320, WIDTH * 80 / 320, WIDTH * 20 / 320);
            self.daView.frame = CGRectMake(0, -WIDTH * 260 / 320, WIDTH, WIDTH * 260 / 320);
            self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 260 / 320, 0, 0, 0);
            self.detaCollectionView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 113);
            
            [_openImageView release];
            [self.closeImageView removeFromSuperview];
            
            self.isReturn = YES;
        }
    }
}

#pragma mark 改变CollectionViewCell的点击方法
- (void)changeClick:(UIButton *)button
{
    if (self.isReturn == YES) {
        if (button.tag == 1002) {
            if (self.detailsStyle == similarStyle) {
                self.detailsStyle = listStyle;
                self.detailsFlowLayout.itemSize = CGSizeMake((WIDTH - 40) / 4, (WIDTH - 40) / 4 * 30 / 83);
                self.detailsFlowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 69, 5);
                self.siGeKongjianView.hidden = NO;
                self.daView.frame = CGRectMake(0, -WIDTH * 285 / 320, WIDTH, WIDTH * 285 / 320);
                self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 285 / 320, 0, 0, 0);
                self.detaCollectionView.contentOffset = CGPointMake(0, - self.daView.frame.size.height);
                [self.detaCollectionView reloadData];
            }
        } else if (button.tag == 1003) {
            if (self.detailsStyle == listStyle) {
                self.detailsStyle = similarStyle;
                self.detailsFlowLayout.itemSize = CGSizeMake((WIDTH - 50) / 4, (WIDTH - 50) / 4 * 110 / 65);
                self.detailsFlowLayout.minimumLineSpacing = 10;
                self.detailsFlowLayout.minimumInteritemSpacing = 10;
                self.detailsFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 69, 10);
                self.siGeKongjianView.hidden = YES;
                self.daView.frame = CGRectMake(0, -WIDTH * 260 / 320, WIDTH, WIDTH * 260 / 320);
                self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 260 / 320, 0, 0, 0);
                [self.detaCollectionView reloadData];
            }
        }
    }else if (self.isReturn == NO){
        if (button.tag == 1002) {
            if (self.detailsStyle == similarStyle) {
                self.detailsStyle = listStyle;
                self.detailsFlowLayout.itemSize = CGSizeMake((WIDTH - 40) / 4, (WIDTH - 40) / 4 * 30 / 83);
                self.detailsFlowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 69, 5);
                self.siGeKongjianView.hidden = NO;
                // 计算文字的高度
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
                self.detailsLabel.numberOfLines = 0;
                [self.detailsLabel sizeToFit];
                CGRect rect = [self.detailsLabel.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
                self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, rect.size.height);
                self.daView.frame = CGRectMake(0, -WIDTH * 270 / 320 - self.detailsLabel.frame.size.height, WIDTH, WIDTH * 270 / 320 + self.detailsLabel.frame.size.height);
                self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 270 / 320 + self.detailsLabel.frame.size.height, 0, 0, 0);
                self.detaCollectionView.contentOffset = CGPointMake(0, - self.daView.frame.size.height);
                [self.detaCollectionView reloadData];
            }
        } else if (button.tag == 1003) {
            if (self.detailsStyle == listStyle) {
                self.detailsStyle = similarStyle;
                self.detailsFlowLayout.itemSize = CGSizeMake((WIDTH - 50) / 4, (WIDTH - 50) / 4 * 110 / 65);
                self.detailsFlowLayout.minimumLineSpacing = 10;
                self.detailsFlowLayout.minimumInteritemSpacing = 10;
                self.detailsFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 69, 10);
                self.siGeKongjianView.hidden = YES;
                // 计算文字的高度
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
                self.detailsLabel.numberOfLines = 0;
                [self.detailsLabel sizeToFit];
                CGRect rect = [self.detailsLabel.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
                self.detailsLabel.frame = CGRectMake(WIDTH * 10 / 320, WIDTH * 190 / 320, WIDTH - 20, rect.size.height);
                self.daView.frame = CGRectMake(0, -WIDTH * 245 / 320 - self.detailsLabel.frame.size.height, WIDTH, WIDTH * 245 / 320 + self.detailsLabel.frame.size.height);
                self.detaCollectionView.contentInset = UIEdgeInsetsMake(WIDTH * 245 / 320 + self.detailsLabel.frame.size.height, 0, 0, 0);
                [self.detaCollectionView reloadData];
            }
        }
    }
    
}


#pragma mark collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.detailsStyle == listStyle) {
        AnimeReadViewController *anReadVC = [[AnimeReadViewController alloc] init];
        [anReadVC setModalTransitionStyle:2];
        DetailsListModel *listModel = self.listArr[indexPath.row];
        anReadVC.epId = self.Id;
        anReadVC.listId = listModel.listId;
        anReadVC.readTitle = self.tuTitle;
        [self presentViewController:anReadVC animated:YES completion:^{
            
        }];
        [anReadVC release];
    } else if(self.detailsStyle == similarStyle){
        AnimeDetallsViewController *detaVC = [[AnimeDetallsViewController alloc] init];
        [self.navigationController pushViewController:detaVC animated:YES];
        DetailsSimilarModel *similarModel = self.similarArr[indexPath.row];
        detaVC.Id = similarModel.Id;
        detaVC.tuTitle = similarModel.title;
        [detaVC release];
    }
}

#pragma mark 两个button的点击方法
- (void)shoucangClick:(UIButton *)button
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"anime.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    NSString *collcetCountBoxPath = [sandBoxPath stringByAppendingPathComponent:@"collcetCount.txt"];
    if (self.isArchiver) {
        NSInteger index = 0;
        for (NSInteger i = 0; i < arr.count; i++) {
            DetailsInfoModel *infoModel = arr[i];
            if ([infoModel.infoId isEqualToString:self.Id]) {
                index = i;
                break;
            }
        }
        [arr removeObjectAtIndex:index];
        [NSKeyedArchiver archiveRootObject:arr toFile:documentPath];
        [self.shoucangButton setTitle:@"点击收藏" forState:UIControlStateNormal];
        self.isArchiver = NO;
        
        [DownloadSingleton shareDownloadSingleton].collcetCount = [DownloadSingleton shareDownloadSingleton].collcetCount - 1;
        NSString *str = [NSString stringWithFormat:@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount];
        [str writeToFile:collcetCountBoxPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount);
        
        
    }else{
        if (arr != nil) {
            [arr addObject:self.infoModel];
        } else {
            arr = [NSMutableArray array];
            [arr addObject:self.infoModel];
        }
        [NSKeyedArchiver archiveRootObject:arr toFile:documentPath];
        [self.shoucangButton setTitle:@"已收藏" forState:UIControlStateNormal];
        self.isArchiver = YES;
        
        [DownloadSingleton shareDownloadSingleton].collcetCount = [DownloadSingleton shareDownloadSingleton].collcetCount + 1;
        NSString *str = [NSString stringWithFormat:@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount];
        [str writeToFile:collcetCountBoxPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount);
       
    }
    
    NSIndexPath *leftIndexPath = [DownloadSingleton shareDownloadSingleton].leftIndexPath;
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LeftCollectionViewCell *cell = (LeftCollectionViewCell*)[tempAppDelegate.homeVC.collectionView cellForItemAtIndexPath:leftIndexPath];
    [cell.tableView reloadData];
}

#pragma mark 点击阅读
- (void)yueduClick:(UIButton *)button
{
    AnimeReadViewController *readVC = [[AnimeReadViewController alloc] init];
    DetailsListModel *listModel = self.listArr.firstObject;
    readVC.epId = self.Id;
    readVC.listId = listModel.listId;
    readVC.readTitle = self.title;
    [self setModalTransitionStyle:2];
    [self presentViewController:readVC animated:YES completion:^{
        
        
    }];
    [readVC release];
}

#pragma mark 左右两个button的点击方法

- (void)leftClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = 0;
    switch (self.detailsStyle) {
        case listStyle:
            count = self.listArr.count;
            break;
        case similarStyle:
            count = self.similarArr.count;
            break;
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.detailsStyle == listStyle) {
        DetaCollectionViewCell *detaCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        DetailsListModel *listModel = nil;
        if (self.isZheng == YES) {
            listModel = self.listArr[(self.listArr.count - 1) - indexPath.row];
        } else {
            listModel = self.listArr[indexPath.row];
        }
        detaCell.detaLabel.text = listModel.chapter_name;
        return detaCell;
    } else {
        TuijianCollectionViewCell *tuijianCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tuijianReuse" forIndexPath:indexPath];
        DetailsSimilarModel *similarModel = self.similarArr[indexPath.row];
        [tuijianCell.picImageView sd_setImageWithURL:[NSURL URLWithString:similarModel.cover] placeholderImage:[UIImage imageNamed:@"漫画占位图"]];
        tuijianCell.tuLabel.text = similarModel.title;
        return tuijianCell;
    }
}

#pragma mark 反归档
- (void)fanguidang
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"anime.plist"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    self.isArchiver = NO;
    if (arr != nil) {
        for (DetailsInfoModel *infoModel in arr) {
            if ([infoModel.title isEqualToString:self.tuTitle]) {
                self.isArchiver = YES;
                break;
            }
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
